<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@taglib uri="/WEB-INF/page-base.tld" prefix="w" %>
<%
    String path = request.getContextPath();
%>
<style>
    .btn {
        text-decoration: none;
        background-color: #00A4F2;
        color: #fff;
        padding: 5px 20px;
        border-radius: 5px;
    }

    .btn:hover {
        background-color: #00ADFF;
    }

    .ui-btn {
        padding: 15px;
    }

    .inpradio {
        margin-top: 0;
    }

    .datainp {
        width: 173px;
        height: 30px !important;
        border: 1px #A5D2EC solid;
    }

    .wicon {
        background-image: url(
        "");
        background-repeat: no-repeat;
        background-position: right center;
    }

    #errormsg {
        position: fixed;
        width: 200px;
        height: 50px;
        line-height: 50px;
        padding: 1px 5px;
        text-align: center;
        background-color: #00A4F2;
        opacity: 0.5;
        color: #fff;
        left: 50%;
        margin-left: -50px;
        top: 50%;
        border-radius: 20px;
    }

    .radio-top {
        margin-top: -2px;
    }
</style>
<form id="buyform" method="post" action="/marketing/BuySendPromotionPage@chooseProductList.page">
    <div id="errormsg" style="display: none;"></div>
    <div class="headT">
        <span></span>
        <label>活动定义</label>
    </div>
    <div class="ui-formContent">
        <p>
            <w:hidden bind="bean.start_time"/>
            <w:hidden bind="bean.end_time"/>
            <w:hidden bind="bean.promotion_type"/>
            <label style="width:130px;text-align:right">推广平台：</label>
            <label class="radioButton"><input id="all" name="platform" checked="checked" class="radio-top" type="radio"
                                              value="0"/>全平台
                <input id="pc" name="platform" type="radio" class="radio-top" value="1"/>PC端
                <input id="wxd" name="platform" type="radio" class="radio-top" value="2"/>微商城</label>
            <font class="red">*</font>
            <label style="width:130px;text-align:right">活动时间：</label>
            <input class="datainp wicon" name="startTime" id="buyEditInpstart" type="text" placeholder="请选择" readonly>到
            <input class="datainp wicon" name="endTime" id="buyEditInpend" type="text" placeholder="请选择" readonly>
            <font class="red">*</font>
        </p>
        <p>
            <label style="width:130px;text-align:right">活动标题：</label>
            <input name="promotionsname" style="width:731px" maxlength="40" styleClass="easyui-validatebox"
                   autocomplete="off"/>
            <font class="red">*</font>
        </p>
        <p>
            <label style="width:130px;text-align:right">活动描述：</label>
            <input name="description" style="width:731px" maxlength="30" styleClass="easyui-validatebox"
                   autocomplete="off"/>
        </p>
        <p>
            <label style="width:130px;text-align:right">排序：</label>
            <input name="sort_value" style="width:100px;" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" maxlength="8" styleClass="easyui-validatebox"
                   autocomplete="off"/>
        </p>
        <p>
            <label style="width:130px;text-align:right">活动专区描述：</label>
            <input name="district_description" style="width:731px" maxlength="30" styleClass="easyui-validatebox"
                   autocomplete="off"/>
        </p>
        <p>
            <label style="width:130px;text-align:right">是否显示今日特卖: </label>
            <input id="hide_sell_id" name="show_sell" type="radio" checked="checked" class="radio-top" value="0"/>不显示
            <input id="show_sell_id" name="show_sell" type="radio" class="radio-top" value="1"/>显示
        </p>
    </div>
    <div class="headT">
        <span></span>
        <label>规则限制</label>
    </div>
    <div class="ui-formContent">
        <p>
            <label style="width:130px;text-align:right">活动规则：</label>
            买<input name="buy_number" maxlength='7' styleClass="easyui-validatebox" style="width:100px;margin: 0 5px;"
                    onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
                    autocomplete="off"/>件
            <font class="red">*</font>
            送<input name="send_number" maxlength='7' styleClass="easyui-validatebox" style="width:100px;margin: 0 5px;"
                    onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
                    autocomplete="off"/>件
            <font class="red">*</font>
        </p>
        <p>
            <label style="width:130px;text-align:right">参与次数：</label>
            <input name="time" maxlength='7' style="width:100px;margin: 0 18px;"
                   onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
                   autocomplete="off"/>次
        </p>
        <p>
            <label style="width:130px;text-align:right;float: left;">客户范围：</label>
            <select name="controlsales" id="controlsales"
                    style="width:386px;height:200px;border:1px solid #DDDDDD;margin-left: 9px;" multiple="multiple">
                <w:iterate id="info" bind="#controlsales">
                    <option value="{'type':'<w:write bind="#info.type"/>','val':'<w:write bind="#info.val"/>','text':'<w:write bind="#info.text"/>'}">
                        <w:write bind="#info.text"/></option>
                </w:iterate>
            </select>
        </p>
        <div style="margin-top:8px;margin-left: 149px;">
            <a href="javascript:void(0);" class="ui_btn marginR8" onclick="addControlsales()">新增控销</a>
            <a href="javascript:void(0);" class="ui_btn marginR8" onclick="delControlsales()">删除控销</a>
        </div>
    </div>

    <div style="width: 100%;  text-align: center; position: absolute;z-index: 10000;bottom:0;"
         class="ui-buttonBar ui-btn">
        <%--<a href="#" class="btn">完成</a>--%>
        <input type="submit" name="submit" value="完成"/>
    </div>

    <div class="headT">
        <span></span>
        <label>选择商品</label>
    </div>
    <div class="easyui-panel ui-toolBar" border="false" data-options="doSize:false">
        <table cellpadding="0" cellspacing="0" style="margin-top:5px">
            <tr>
                <td width="70" align="right">商品名称：</td>
                <td class="null"><w:text bind="bean.pname" style="width:120px;height: 26px;margin-right: 5px;"/></td>
                <td width="70" align="right">商品分类：</td>
                <td class="null"><select id="classtree" name="bean.classid" class="easyui-combotree"
                                         style="width:250px;height:28px;"
                                         data-options="url:'<%=path %>/products/ProductClassPage@loadClassTree2.page'"></select>
                </td>


                <td width="70" align="right">商品编码：</td>
                <td><w:text bind="bean.code" style="width:200px;height:26px;"
                            onchange="value=(value.match(/\d+/g)||['']).join(',')"/></td>
                <td>
                    &nbsp;<a href="javascript:;" class="easyui-linkbutton" iconCls="icon_search"
                             onclick="query1()">查询</a>
                    &nbsp;<a href="javascript:;" class="easyui-linkbutton" iconCls="icon_search" onclick="resetform()">重置</a>
                    &nbsp;<a href="javascript:;" class="easyui-linkbutton" id="see" iconCls="icon_search"
                             onclick="addpro()">查看已添加商品</a>
                </td>
            </tr>
        </table>
    </div>
</form>
<div class="ui-formContent">
    <div data-options="region:'center',split:true">
        <div id="chooseShowListPanel" class="easyui-panel" border="false" data-options="doSize:false,href:''">
        </div>
    </div>
</div>
<div id="seestab" style="border: 1px solid #DDDDDD;width:1200px;display: none;">

</div>
<div style="width: 100%;height: 100px;"/>
<script>
    $(function () {
        $(function () {
            $("a.panel-tool-close").unbind("click");
            $("a.panel-tool-close").click(function (e) {
                if (window.confirm("确认取消添加买送促销活动?")) {
                    window.location.reload();
                }
            });
        })
    })


    function query1() {
        $w.execForm({form: $('#buyform'), target: $('#chooseShowListPanel'), pageNum: 1})
    }
    function resetform() {
        $(".ui-toolBar input").val("");
    }
    function chooseShowList() {
        $w.execUrl(ctx + "/marketing/BuySendPromotionPage@chooseProductList.page", {}, $('#chooseShowListPanel'));
    }
    $(function () {
        var start = $("[name='bean.start_time']").val();
        $("[name='starttime']").val(start)
        var end = $("[name='bean.end_time']").val();
        $("[name='endtime']").val(end);
        $("#show_sell_id").prop("checked", true);
        chooseShowList();
    });
    function pedit_prodoctupdate() {
//        $.messager.progress();
        pedit_saveProduct();
    }
    var pedit_saveing = false;
    function pedit_saveProduct() {
        if (!pedit_saveing) {
            pedit_saveing = true;//防止重复保存
            var data = productdata();
            $w.doUrl(ctx + "/marketing/BuySendPromotionPage@update.page", data, function (ret) {

                $.messager.progress('close');
                if (ret.state == 'ok') {
                    window.parent.$w.correctMsg("保存商品成功");
                    $w.closeCurrWin("ok");
                } else window.parent.$w.alertMsg(ret.state);
            })
        }
    }
    //组合数据
    function productdata() {

        var win = $w.currWin();
        var data = {
            "bean.promotionid": $("[name='bean.promotionid']").val(),
            "bean.start_time": $("#buyEditInpstart").val(),
            "bean.end_time": $("#buyEditInpend").val(),
            "bean.name": $("[name='promotionsname']").val(),
            "bean.description": $("[name='description']").val(),
            "bean.buy_number": $("[name='buy_number']").val(),
            "bean.send_number": $("[name='send_number']").val(),
            "bean.time": $("[name='time']").val(),
            "bean.sort_value": $("[name='sort_value']").val(),
            "bean.district_description": $("[name='district_description']").val(),
        };
        data["bean.platform"] = $("input[name='platform']:checked").val();
        data["bean.product_val"] = $("input[name='primaryKey']:checked").val();


        data["bean.show_sell"] = $("input[name='show_sell']:checked").val();


        var controlsales = "";//控销数据

        var combox = $("#controlsales").get(0);
        for (var i = 0; i < combox.options.length; i++) {
            if (controlsales != "") controlsales += ",";
            controlsales += combox.options[i].value;
        }
        data["bean.controlsales"] = "[" + controlsales + "]";
        var controlusers = "";//控销用户
        $("[name='controluserid']").each(function () {
            controlusers += this.value + ",";
        })
//        data["bean.controlusers"]=controlusers
        return data;
    }


    var productItem = {};
    function inp(obj) {

        productItem = {};
        //获取商品属性
        var productPromot = $(obj).parents("tr");
        productItem.showlistid = productPromot.find("input[name='showlistid']").val();
        productItem.showlistname = productPromot.find("input[name='showlistname']").val();
        productItem.showlistcode = productPromot.find("input[name='showlistcode']").val();
        productItem.showlistB_chanjia = productPromot.find("input[name='showlistB_chanjia']").val();
        productItem.showlistB_guige = productPromot.find("input[name='showlistB_guige']").val();
        productItem.showlistclassname = productPromot.find("input[name='showlistclassname']").val();
        productItem.showlistproductamount = productPromot.find("input[name='showlistproductamount']").val();
        productItem.showlisttypes = productPromot.find("input[name='showlisttypes']").val();
        $("#pro").show();
    }
    function addpro() {
        if (productItem["showlistcode"] == null) {
            $("#pro").hide();
        }
        if (productItem) {
            $("#td1").text(productItem["showlistcode"]);
            $("#td2").text(productItem["showlistname"]);
            $("#td3").text(productItem["showlistB_chanjia"]);
            $("#td4").text(productItem["showlistB_guige"]);
            $("#td5").text(productItem["showlistclassname"]);
            $("#td6").text(productItem["showlistproductamount"]);
            $("#td7").text(productItem["showlisttypes"]);
        } else {
            $("#prolist").toggleClass("classpro");
        }
    }

    function deletepro(obj) {
        $("#pro").hide();
        $("#prolist input").prop("checked", false);
    }
</script>
<script>
    /*信息不完整，创建活动失败*/
    function Createfailure() {
        $w.errorMsg("信息不完整，创建活动失败!");
    }
    /*活动创建成功*/
    function Createsuccess() {
        $("#errormsg").html("活动创建成功").fadeIn(300).delay(2000).fadeOut(300);
    }
    /*活动未创建成功，直接离开页面？*/
    function Notcreated() {
        window.parent.$w.confirmMsg("活动未创建成功，直接离开页面？", function () {

        })
    }
    /*点击完成按钮验证必填项*/
    $(function () {
        /*当前表单调用validate方法，实现表单验证功能*/
        $("#buyform").validate({
            debug: true, /*调试模式，验证通过也不跳转到目标页面，如要提交跳转修改成false*/
            rules: {
                /*配置验证规则*/
                platform: {
                    required: true/*必填。如果验证方法不需要参数，则配置为true*/
                },
                startTime: {
                    required: true
                },
                endTime: {
                    required: true
                },
                promotionsname: {
                    required: true
                },
                buy_number: {
                    required: true
                },
                send_number: {
                    required: true
                }
            },
            messages: {
                platform: {
                    required: "推广平台不能为空，必选一项!"
                },
                startTime: {
                    required: "活动开始时间不能为空"
                },
                endTime: {
                    required: "活动结束时间不能为空"
                },
                promotionsname: {
                    required: "活动标题不能为空"
                },
                buy_number: {
                    required: "买多少件不能为空"
                },
                send_number: {
                    required: "送多少件不能为空"
                },
            },
            /* 重写错误显示消息方法,以吐司方式弹出错误消息 */
            /*获取单项必填项消息*/
            showErrors: function (errorMap, errorList) {
                var msg = "";
                $.each(errorList, function (i, v) {
                    if (i == 0)//每次只弹出一个提示
                    {
                        msg += (v.message + "\r\n");
                    }
                });
                if (msg != "")
//                    toastr.warning(msg) || toastr.success(msg)
                    $("#errormsg").html(msg).fadeIn(300).delay(2000).fadeOut(300);
            },
            /* 失去焦点时不验证 */
            onfocusout: false,
            submitHandler: function (form) {
                /*执行提交的函数*/
                pedit_prodoctupdate();
            }
        });
    });
    //删除控销用户
    function delcontroluser(obj) {
        $(obj).parent().parent().remove();
    }
    //添加控销
    function addControlsales() {
        window.parent.$w.openDialog({
            url: ctx + '/products/ProductPage@addControlsalesShow.page',
            title: "活动控销",
            w: 600,
            h: 400,
            afterClose: function (ret) {
                if (ret && ret.list.length > 0) {
                    var txt = "";
                    var combox = $("#controlsales").get(0);
                    for (var i = 0; i < ret.list.length; i++) {
                        if (ret.type == "1") txt = "客户分组：" + ret.list[i].text;
                        else if (ret.type == "2") txt = "价格等级：" + ret.list[i].text;
                        else if (ret.type == "3") txt = "区域控销：" + ret.list[i].text;
                        val = "{'type':'" + ret.type + "','val':'" + ret.list[i].val + "','text':'" + txt + "'}";
                        combox.options[combox.options.length] = new Option(txt, val);
                    }
                }
            }
        })
    }
    function delControlsales() {
        var combox = $("#controlsales").get(0);
        if (combox.options.length > 0) {
            for (var i = 0; i < combox.options.length; i++) {
                var option = combox.options[i];
                if (option.selected) {
                    combox.options[i] = null;
                    i--;
                }
            }
        }
    }
    //点击查看已添加的商品
    $.fn.toggle = function (fn) {
        var args = arguments,
            guid = fn.guid || jQuery.guid++,
            i = 0,
            toggler = function (event) {
                var lastToggle = ( jQuery._data(this, "lastToggle" + fn.guid) || 0 ) % i;
                jQuery._data(this, "lastToggle" + fn.guid, lastToggle + 1);
                event.preventDefault();
                return args[lastToggle].apply(this, arguments) || false;
            };
        toggler.guid = guid;
        while (i < args.length) {
            args[i++].guid = guid;
        }
        return this.click(toggler);
    }
    $(function () {
        $("#see").toggle(
            function () {
                $("#seetab").hide();
                $("#see").css("background-color", " #C9C9C9");
                $("#seestab").show();
            },
            function () {
                $("#seetab").show();
                $("#see").removeAttr("style");
                $("#seestab").hide();
            }
        )
    })
    /*点击查询*/
    $("#search").click(function () {
        $("#seetab").show();
        $("#seestab").hide();
        $("#see").removeAttr("style");
    })
    //实现日期选择联动
    var start = {
        format: 'YYYY-MM-DD hh:mm:ss',//日期格式
        minDate: $.nowDate({DD: 0}), //设定最小日期为当前日期
        maxDate: '2099-06-16 23:59:59', //最大日期
        festival: true,//是否显示节日
        isClaer: true,//是否显示清空
        isinitVal: false,//是否初始化时间
        zIndex: 300000,//层级高度
        choosefun: function (elem, datas) {
            end.minDate = datas; //开始日选好后，重置结束日的最小日期
            endDates();
        },//选中日期回调
        okfun: function (elem, datas) {
            //alert(datas)
        }//点击确定回调
    };
    var end = {
        format: 'YYYY-MM-DD hh:mm:ss',//日期格式
        minDate: $.nowDate({DD: 0}), //设定最小日期为当前日期
        maxDate: '2099-06-16 23:59:59', //最大日期
        festival: true,//是否显示节日
        isClaer: true,//是否显示清空
        isinitVal: false,//是否初始化时间
        zIndex: 300000,//层级高度
        choosefun: function (elem, datas) {
            start.maxDate = datas; //将结束日的初始值设定为开始日的最大日期
        }
    };
    //日期联动
    function endDates() {
        end.trigger = false; //结束日期的事件改成false
        $("#buyEditInpend").jeDate(end);
    }
    $("#buyEditInpstart").jeDate(start);
    $("#buyEditInpend").jeDate(end);
</script>